import { Component, OnInit } from '@angular/core';
import { GridOptions } from '@ag-grid-community/core';
import { GridConfigService } from '@core/config/grid-config.service';
import { ExportExcelService } from '@core/services/excel/export-excel.service';
import { ApiService } from '@core/services/http/api.service';
import {ApesMessageService} from "@shared/apes/message";
import {PurchaseService} from "../purchase.service";
import {UserAuthService} from "@core/services/user-auth.service";
import { AllModules, Module } from '@ag-grid-enterprise/all-modules';

@Component({
  selector: 'transfer-station-factory',
  templateUrl: './transfer-station-factory.component.html',
  styleUrls: ['../purchase-report-form.scss']
})
export class TransferStationFactoryComponent implements OnInit {
  /* 按钮相关 */
  buttonBar = [
    { hide: true, style: 'default', label: '导出', id: 'action_export' },
    { hide: true, style: 'default', label: '查询', id: 'action_query' ,loading: false},
    { hide: true, style: 'default', label: '重置', id: 'action_reset' },
    // { hide: true, style: 'default', label: '展开查询条件', id: 'action_show' },
  ];
  title = '';
  ctx = { model: {} };

  /** Grid 相关 **/
  rowData: any;
  resultData: any;
  gridOptions: GridOptions;
  pinnedBottomRowData: any[] = [];
  columnDefs: any[] = [];
  rowClassRules :any;
  height: any = window.innerHeight - 285 + 'px';
  public modules: Module[] = AllModules;

  // 报错样式
  hind: string = 'none';
  // show: string = 'none';           // 展开查询条件

  // 获取供应商
  supplier: any;

  mdjcTime: any = [];       // 日期
  startValue: Date = null;
  endValue: Date = null;
  supplierID: string;                 // 供应商
  checkOne: boolean = true;          // 供应商未确认
  checkTwo: boolean = false;         // 已超时
  userInfo = this.userAuthService.getUserInfoObj();
  companyId = this.userInfo.userAccount.post.company.id;
  permitId = this.userInfo.userAccount.post.localPermit.id;
  companyData:any;
  companyList: any[] = [];
  companyClone: any[] = [];

  constructor(private apiService: ApiService,
              private gridConfigService: GridConfigService,
              private exportExcelService: ExportExcelService,
              private msg: ApesMessageService,
              private pa: PurchaseService,
              private userAuthService: UserAuthService) {
    this.gridInit();
  }

  ngOnInit() {
    this.initialization();
  }

  // gird初始化
  gridInit() {
    this.columnDefs = [
      { 'headerName': '供应商编码', field: 'GYSID' },
      { 'headerName': '供应商名称', field: 'GYSMC'},
      { 'headerName': '配件名称', field: 'PJMC'},
      { 'headerName': '商品编码', field: 'SPID' },
      { 'headerName': '商品名称', field: 'SPMC' },
      { 'headerName': '耗时', field: 'HS',cellRenderer:this.pa.toTime },
      { 'headerName': '超时', field: 'TIO',cellRenderer:this.pa.toTime },
      { 'headerName': '返厂原因', field: 'FCTHYYMC' },
      { 'headerName': '制单时间', field: 'ZDRQ_SJ' },
      { 'headerName': '发货时间', field: 'FHRQ_SJ' },
      { 'headerName': '签收时间', field: 'QSRQ_SJ' },
      { 'headerName': '采购员', field: 'RYMC' },
      { 'headerName': '供应商确认时间', field: 'QRRQ_SJ' },
      { 'headerName': '返厂单号', field: 'LYDJHM' },
      { 'headerName': '返厂出库单号', field: 'CKDH' }
    ];

    // 初始化 grid
    this.gridOptions = <GridOptions>{
      toolPanelSuppressSideButtons: true,
      suppressExcelExport: true,
      suppressCsvExport: true,
      suppressSizeToFit: true,
      showToolPanel: false,
      pagination: true,
      paginationPageSize: 100,
      rowHeight: 35,
    };
    this.gridOptions.localeText = this.gridConfigService.getLocaleText();
    this.rowClassRules = {
      // 预警
      'grid-tab-breach': function(params) {
        let now = params.data.TIO;
        return now < 0;
      },
      // 超时
      'grid-tab-warning': function(params) {
        let now = params.data.TIO;
        return now >= 0;
      },
    };
  }

  // 初始化
  initialization() {
    this.pa.getCompany(this.permitId).then(value => {
      this.companyData = value;
      this.companyClone = this.companyData.map( item => {
        return item.id;
      });
      this.queryData();
    });
    this.pa.getSupplierData().then(value => { this.supplier = value });
  }

  onEvent(data: any) {

    if (data instanceof MouseEvent) return;
    if (data) {
      if (data.id === 'action_query') {
        this.buttonBar[1].loading = true;
        this.queryData();
      } else if (data.id === 'action_reset') {
        this.action_reset();
      } else if (data.id === 'action_export') {
        this.action_export();
      }
      // else if (data.id === 'action_show') {
      //   this.action_show();
      // }
    }
  }

  // 导出数据
  action_export() {
    if (this.rowData && this.rowData.length === 0) {
      return;
    }
    for(let i = 0 , len=this.rowData.length; i< len ; i++){
      this.rowData[i]['HS'] = this.pa.toTime(this.rowData[i]['HS']);
      this.rowData[i]['TIO'] = this.pa.toTime(this.rowData[i]['TIO']);
    }
    const columnList = this.gridOptions.columnDefs;
    this.exportExcelService.exportWithDealData(columnList, this.rowData, '中转站返工监控');
  }

  // 重置
  action_reset(): void {
    this.mdjcTime = '';
    this.supplierID = '';
    this.checkOne = true;
    this.checkTwo = false;
    this.companyList = [];
  }

  // 查询按钮事件
  queryData() {
    let list = [];
    if(this.companyList.length <= 0){
      list = this.companyClone;
    }else {
      list = this.companyList;
    }
    this.rowData = [];
    this.pa.getDate(this.mdjcTime, 'startValue', 'endValue', this);
    const data = {
      'interfaceMethod': 'ipmPurchareControlReportService.queryFactorySupplier', 'pdata': {
        'START_TYRQ_SJ': this.startValue,
        'END_TYRQ_SJ': this.endValue,
        'GYSID': this.supplierID,
        'TYBJ': this.checkOne,
        'YCSBJ': this.checkTwo,
        'companyId': list
      },
    };
    this.pa.getTablesData(data).then(value => { this.rowData = value });
    this.buttonBar[1].loading = false;
  }
}
